package com.cloudweb.oa.utils;

import cn.js.fan.util.file.FileUtil;
import com.cloudweb.oa.utils.SpringUtil;
import com.cloudwebsoft.framework.util.LogUtil;
import com.redmoon.oa.util.Word2Html;
import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.document.DefaultDocumentFormatRegistry;
import org.jodconverter.core.office.OfficeException;
import org.springframework.stereotype.Component;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

@Component
public class Office2Html {

    public void toHtml(String sourceFilePath) {
        String wordName = FileUtil.getFileName(sourceFilePath);
        String wordPath = sourceFilePath.substring(0, sourceFilePath.lastIndexOf(wordName) - 1);
        String name = FileUtil.getFileNameWithoutExt(wordName);
        String htmlName = name + ".html";
        final String imagePath = wordPath + File.separator + name + ".files";
        // 判断html文件是否存在，如存在则不作处理
        // 如果存在，当套红后也需重新生成
        File htmlFile = new File(wordPath + File.separator + htmlName);
        /*if (htmlFile.exists()) {
            return;
        }*/

        DocumentConverter documentConverter = (DocumentConverter) SpringUtil.getBean("documentConverter");
        File sourceFile = new File(sourceFilePath);
        BufferedReader br = null;
        // 开始转换
        try {
            documentConverter.convert(sourceFile)
                    .to(htmlFile).as(DefaultDocumentFormatRegistry.HTML)
                    .execute();

            StringBuilder htmlSb = new StringBuilder();
            br = new BufferedReader(new InputStreamReader(new FileInputStream(htmlFile)));
            while (br.ready()) {
                htmlSb.append(br.readLine());
            }

            File imgPath = new File(imagePath);
            if (!imgPath.exists()) {// 图片目录不存在则创建
                imgPath.mkdirs();
            }

            // 调整图片地址
            String htmlStr = htmlSb.toString();
            htmlStr = htmlStr.replaceAll("<img src=\"", "<img src=\"" + name + ".files" + "/");

            FileUtil.WriteFileUTF8(wordPath + File.separator + htmlName, htmlStr);

            // 移动图片至目录 imagePath
            File folder = new File(wordPath);
            if (folder.isDirectory()) {
                File[] files = folder.listFiles();
                if (files != null) {
                    for (File file : files) {
                        if (!file.isDirectory()) {
                            LogUtil.getLog(file.getAbsolutePath() + "---" + imagePath + File.separator + file.getName());
                            if (file.getName().startsWith(name + "_html_")) {
                                Path sourcePath = Paths.get(file.getAbsolutePath());
                                Path targetPath = Paths.get(imagePath + File.separator + file.getName());
                                // 移动文件，如果目标文件已存在，则替换它
                                Files.move(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
                            }
                        }
                    }
                }
            }
        } catch (OfficeException | IOException e) {
            LogUtil.getLog(Office2Html.class).error(e);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    LogUtil.getLog(Office2Html.class).error(e);
                }
            }
        }
    }
}
